package com.example.cardgame

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.io.FileOutputStream

class SecondActivity : AppCompatActivity() {

    private lateinit var sharedPreferences: SharedPreferences
    private lateinit var nameEditText: EditText
    private lateinit var genderEditText: EditText
    private lateinit var studentIdEditText: EditText
    private lateinit var phoneEditText: EditText
    private lateinit var addressEditText: EditText
    private lateinit var hobbiesEditText: EditText

    private lateinit var imageView: ImageView
    private val REQUEST_IMAGE_CAPTURE = 1
    private var imagePath: String ? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)


        sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE)
        imageView = findViewById(R.id.imageView2)

        nameEditText = findViewById(R.id.nameEditText)
        genderEditText = findViewById(R.id.genderEditText)
        studentIdEditText = findViewById(R.id.studentIdEditText)
        phoneEditText = findViewById(R.id.phoneEditText)
        addressEditText = findViewById(R.id.addressEditText)
        hobbiesEditText = findViewById(R.id.hobbiesEditText)

        loadUserInfo()
        //拍照
        findViewById<Button>(R.id.button).setOnClickListener {
//            Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
//                takePictureIntent.resolveActivity(packageManager)?.also {
//                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
//                }
//            }
            dispatchTakePictureIntent()
        }
        findViewById<Button>(R.id.saveButton).setOnClickListener {
            saveUserInfo()
            setResult(Activity.RESULT_OK)
            finish()
        }
    }
    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            imageView.setImageBitmap(imageBitmap)
            saveImageToInternalStorage(imageBitmap)
        }
    }



    private fun saveImageToInternalStorage(bitmap: Bitmap) {
        val file = File(filesDir, "profile_image.jpg")
        FileOutputStream(file).use { out ->
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
        }
        imagePath = file.absolutePath
    }


    private fun loadUserInfo() {
        nameEditText.setText(sharedPreferences.getString("name", ""))
        genderEditText.setText(sharedPreferences.getString("gender", ""))
        studentIdEditText.setText(sharedPreferences.getString("studentId", ""))
        phoneEditText.setText(sharedPreferences.getString("phone", ""))
        addressEditText.setText(sharedPreferences.getString("address", ""))
        hobbiesEditText.setText(sharedPreferences.getString("hobbies", ""))
        val imagePath = sharedPreferences.getString("profile_image_path", null)
        if (imagePath != null) {
            val bitmap = BitmapFactory.decodeFile(imagePath)
            imageView.setImageBitmap(bitmap)
        }
    }

    private fun saveUserInfo() {
        val editor = sharedPreferences.edit()
        editor.putString("name", nameEditText.text.toString())
        editor.putString("gender", genderEditText.text.toString())
        editor.putString("studentId", studentIdEditText.text.toString())
        editor.putString("phone", phoneEditText.text.toString())
        editor.putString("address", addressEditText.text.toString())
        editor.putString("hobbies", hobbiesEditText.text.toString())
        editor.putString("profile_image_path", imagePath)
        editor.apply()
    }
}

